package logo.utils;

import java.awt.*;

@SuppressWarnings("serial")
public class VectorDisplay extends Canvas{
    /** Object for displaying collections of lines that are specified
	in virtual coordinates (not pixel coordinates). **/

    /**
       Virtual coordinates of extreme points of the display
	Coordinates are in the range [0,1].
    **/

    double left = 0F;
    double bottom = 0F;
    double right = 1F;
    double top = 1F;
    
    double xscale, yscale;	// scale factors to convert coords to pixels
    int ymax;			// max pixel y coordinate

    /** xscale, yscale, ymax must be updated automatically if the window
	is resized **/
    /* need to catch an AWTEvent (ComponentEvent) to resize them properly */

    Container myContainer;	// container for this display
    // Graphics gc;		// graphics context
    Image myImage;		// off-screen contents of this display
    
    public VectorDisplay(int width, int height){
	/** Create a new VectorDisplay object with given width
	    and height **/
	super();
	setSize(width, height);
	xscale = (width - 1)/(right - left);
	yscale = (height - 1)/(top - bottom);
	ymax = height - 1;
	/*
	myContainer = makeDefaultFrame(width, height);
	myContainer.add(this);
	myContainer.setVisible(true);
	//gc = getGraphics();
	*/	
    }
    public void init(int width, int height)
    {
	myImage = createImage(width, height);
    }
    protected Frame makeDefaultFrame(int width, int height) {
	Frame result = new Frame();
	result.setCursor(new Cursor(Cursor.CROSSHAIR_CURSOR));
	result.setSize(width, height);
	return result;
    }

    public void paint(Graphics g) {
	g.drawImage(myImage, 0, 0, this);
    }

    public void clear() {
	Dimension dims = getSize();
	myImage.getGraphics().clearRect(0,0, dims.width, dims.height);
	repaint();
    }
    
    protected int convertX(double x) {
	/** Convert the given x coordinate to pixel coordinate **/
	return (int)((x - left) * xscale);
    }

    protected int convertY(double y) {
	/** Convert the given y coordinate to pixel coordinate **/
	return (int)(ymax - ((y - bottom) * yscale));
    }
    
    protected void setCoordinateLimits(double minX, double minY,
				       double maxX, double maxY) {
	left = minX;
	bottom = minY;
	right = maxX;
	top = maxY;
	xscale = (getSize().width - 1)/(right - left);
	yscale = (getSize().height - 1)/(top - bottom);

    }

    public void drawSegment(double x,double y , double x1, double y1) {
	int startX = convertX(x);
	int startY = convertY(y);
	int endX = convertX(x1);
	int endY = convertY(y1);

	myImage.getGraphics().drawLine(startX, startY, endX, endY);
	repaint();
    }

}
